
package edu.tfsu.team_2208.skylarkim.controller;

import edu.tfsu.team_2208.skylarkim.commons.DataResult;
import edu.tfsu.team_2208.skylarkim.commons.ResultData;
import edu.tfsu.team_2208.skylarkim.mapper.entity.Wordxu;
import edu.tfsu.team_2208.skylarkim.service.HdfsService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.java.Log;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;




@RestController
@Log
@RequestMapping("/Xu/hdfs")
public class HdfsController {

    @Resource
    private HdfsService hdfsService;

    private String demo;

    private String hdfs;


    @Operation(summary = "读取hadoop文件xu")

    @RequestMapping(value = "/read",method = {RequestMethod.GET,RequestMethod.POST})
    public ResultData readHadoopFile(@RequestParam("Xu.path") String path,int start,int limit) {
        List<String> result = hdfsService.readFile(path,start,limit);
        List<DataResult> jsonMap =new ArrayList<>();

        if (result != null) {
            for (String line : result) {
                DataResult dr = new DataResult();
                dr.setName(line.split(",")[0]);
                dr.setValue(line.split(",")[1]);

                jsonMap.add(dr);

            }
            return ResultData.success(jsonMap);
        }
        return ResultData.error(500, "读取文件失败");
    }

    @RequestMapping(value = "/select", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData selectFromDB(@RequestParam("limit") int limit) {
        List<Wordxu> result = hdfsService.selectFromWordxu(limit);
        if (result != null) {
            List<DataResult> jsonMap = new ArrayList<>();
            for (Wordxu word : result) {
                DataResult dr = new DataResult();
                dr.setName(word.getWord()); // 正确调用 setName 方法
                dr.setValue(word.getCount().toString());
                jsonMap.add(dr);
            }
            return ResultData.success(jsonMap);
        }
        return ResultData.error(500, "读取数据库失败");
    }
}









































